package com.hboxs.asl.controller.asl.product;

import com.hboxs.asl.Filter;
import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.asl.BaseController;
import com.hboxs.asl.entity.Ad;
import com.hboxs.asl.entity.AdPosition;
import com.hboxs.asl.entity.Product.OrderType;
import com.hboxs.asl.entity.ProductCategory;
import com.hboxs.asl.service.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Controller - 产品
 */
@Controller("aslProductionTuangouController")
@RequestMapping("/p/tuangou")
public class ProductTuangouControl extends BaseController {

    /**
     * 热门推荐数量
     */
    private static final int hotCount = 5;

    protected Logger logger = Logger.getLogger(getClass().getName());

    @Resource(name = "productServiceImpl")
    private ProductService productService;
    @Resource(name = "productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;
    private AdPositionService adPositionService;
    @Resource(name = "adServiceImpl")
    private AdService adService;
    /**
     * 列表
     */
    @RequestMapping(value = "/l", method = RequestMethod.GET)
    public String list(Long pc, OrderType order, Integer pageNumber, HttpServletRequest request,
                       ModelMap model, HttpSession session) {
        try {
            // 商品分类
            ProductCategory session_productCategory = (ProductCategory) session.getAttribute("productCategory");
            Set<ProductCategory> productCategories = null;
            if (session_productCategory != null) {
                productCategories = session_productCategory.getChildren();
            }
            model.addAttribute("session_productCategory" , session_productCategory);
            model.addAttribute("productCategories", productCategories);

            // 选中的商品分类
            ProductCategory productCategory = productCategoryService.find(pc);
            if (productCategory == null) {
                Iterator<ProductCategory> it = productCategories.iterator();
                if (it.hasNext()){
                    productCategory = it.next();
                }
            }
            model.addAttribute("productCategory", productCategory);

            // 默认每页20个
            if (pageNumber == null) {
                pageNumber = 1;
            }
            // 默认每页20个
            Pageable pageable = new Pageable(pageNumber, null);
            List<Filter> fs = new ArrayList<>();
            fs.add(Filter.eq("isTuangou",true));
            pageable.setFilters(fs);
            model.addAttribute("pageNumber", pageNumber);
            model.addAttribute("page",
                    productService.findPage(productCategory, null, null, null, null, null, null, true, null, null, null, null, order, pageable));


            List<Filter> banner_position_fs = new ArrayList<Filter>();
            banner_position_fs.add(Filter.eq("name", "公告"));
            // banner位置
            AdPosition banner_position = adPositionService.findList(1, banner_position_fs, null).get(0);
            List<Filter> banner_fs = new ArrayList<Filter>();
            banner_fs.add(Filter.eq("adPosition", banner_position));
            List<Ad> banners = adService.findList(null, banner_fs, null);
            if (banners!=null && banners.size()>0){
                model.addAttribute("ad", banners.get(0));
            }else{
                model.addAttribute("ad", null);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/yijing/html/goods-tuangou-list";
    }


    /**
     * 上拉下载
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public
    @ResponseBody
    Object page(Long pc, OrderType order, Integer pageNumber, HttpServletRequest request,
                HttpSession session) {
        try {
            // 商品分类
            ProductCategory session_productCategory = (ProductCategory) session.getAttribute("productCategory");
            Set<ProductCategory> productCategories = null;
            if (session_productCategory != null) {
                productCategories = session_productCategory.getChildren();
            }
            // 选中的商品分类
            ProductCategory productCategory = productCategoryService.find(pc);
            if (productCategory == null) {
                productCategory = productCategories.iterator().next();
            }

            // 默认每页20个
            if (pageNumber == null) {
                pageNumber = 1;
            }
            // 默认每页20个
            Pageable pageable = new Pageable(pageNumber, 1);
            List<Filter> fs = new ArrayList<>();
            fs.add(Filter.eq("isTuangou",true));
            pageable.setFilters(fs);
            return JsonMessage.success(productService.findPage(productCategory, null, null, null, null, null, null, true, null, null, null, null, order, pageable).getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 点击数
     */
    @RequestMapping(value = "/hits/{id}", method = RequestMethod.GET)
    public
    @ResponseBody
    Long hits(@PathVariable Long id) {
        return productService.viewHits(id);
    }

    @RequestMapping(value = "/getproducts",method = RequestMethod.POST)
    public
    @ResponseBody
    String getProducts(ModelMap model,Long productCategoryId){
        model.addAttribute("products",productCategoryService.findList(productCategoryId));
        return "";
    }
}
